import time, random
from threading import Thread,Lock
import threading

tickets = 10


def sale_tickets():
    global tickets
    while True:
        if tickets > 0:
            # 睡眠
            time.sleep(random.random())
            print("%s，售出火车票：%d" % (threading.current_thread().name, tickets))
            tickets -= 1
        else:
            break


if __name__ == '__main__':
    t1 = Thread(target=sale_tickets, name="售票口1")
    t2 = Thread(target=sale_tickets, name="售票口2")
    t3 = Thread(target=sale_tickets, name="售票口3")
    t4 = Thread(target=sale_tickets, name="售票口4")
    t1.start()
    t2.start()
    t3.start()
    t4.start()

    print(threading.active_count())